import http from '@/utils/http'
import {
  UserProfileResponse,
  UserResponse,
  UserProfile,
  UserPhotoResponse,
} from '@/types/data'
import { RootThunkAction } from '@/types/store'

//获取个人信息
export const UserProfil = (): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<UserResponse>('/user')
    // console.log(res)
    dispatch({ type: 'profile/get', payload: res.data.data })
  }
}
//回显个人信息
export const getUserProfile = (): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<UserProfileResponse>('/user/profile')
    dispatch({ type: 'profile/user', payload: res.data.data })
  }
}
//更新用户信息
export const updateProfile = (
  userProfile: Partial<UserProfile>
): RootThunkAction => {
  return async (dispatch) => {
    await http.patch('/user/profile', userProfile)
    dispatch({ type: 'profile/update', payload: userProfile })
  }
}
//修改头像
export const updatePhoto = (data: FormData): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.patch<UserPhotoResponse>('/user/photo', data)
    dispatch({ type: 'profile/photo', payload: res.data.data.photo })
  }
}
